package org.ccay.excel.importer.service.impl.internal;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import org.ccay.core.ioc.Ccay;
import org.ccay.excel.exception.ExcelValidationException;
import org.ccay.excel.importer.ExcelCellError;
import org.ccay.excel.importer.ExcelImportRequest;
import org.ccay.excel.importer.IExcelImportContext;
import org.ccay.excel.importer.ITitleMapper;
import org.ccay.excel.importer.service.IExcelDataConsumer;
import org.ccay.excel.importer.service.IExcelImportSupport;
import org.ccay.excel.meta.ExcelMeta;
import org.ccay.excel.meta.SheetMeta;

/**
 * Excel导入时的上下文信息。
 * 	
 * @author chaowangbang
 * @since Mar 15, 2012
 */
public class ExcelImportContext implements Serializable, IExcelImportContext {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Excel导入请求
	 */
	private ExcelImportRequest request;
	
	/**
	 * Excel导入的support对象
	 */
	private IExcelImportSupport support; 
	
	private ExcelMeta excelMeta;
	
	/** 当前正在处理的Sheet **/
	private SheetMeta sheetMeta;
	
	private IExcelDataConsumer dataConsumer;
	
	/**
	 * 标题、列索引号和VO属性的映射关系表
	 */
	private ITitleMapper titleMapper;
	
	private ResourceBundle resourceBundle;
	
	/** 已经处理过的数据行数 **/
	private int processedCount = 0;
	
	/** 成功导入的数据行数 **/
	private int importedCount = 0;
	
	/** 单元格错误列表 **/
	private List<ExcelCellError> errors = null;
	
	/** 最大允许的错误数 **/
	private int maxAllowedErrors = -1;
	
	private Map<?,?> consumerDataMap = null;
	
	private Map<String, String> nameDisplayNameMap = null;// sheetName sheetDisplayName
	
	/**
	 * @param request
	 * @param support
	 */
	public ExcelImportContext(ExcelImportRequest request, IExcelImportSupport support) {
		this.request = request;
		this.support = support;
		this.errors = new ArrayList<ExcelCellError>(500);
		this.excelMeta = support.getExcelMeta();
	}
	
	public IExcelImportSupport getExcelImportSupport() {
		return this.support;
	}
	
	/** 获取Sheet定义	
	 * @author chaowangbang
	 * @since Mar 26, 2012
	 * @param sheetName
	 * @return
	 */
	public SheetMeta getSheetMeta(String sheetName) {
		return this.excelMeta.getSheetMeta(sheetName);
	}
	
	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.IExcelImportContext#getRequest()
	 */
	public ExcelImportRequest getRequest() {
		return this.request;
	}
	
	public void setMaxAllowedErrors(int maxAllowedErrors) {
		this.maxAllowedErrors = maxAllowedErrors;
	}
	
	/** 获取最大允许的错误数	
	 * @author chaowangbang
	 * @since Mar 26, 2012
	 * @return
	 */
	public int getMaxAllowedErrors() {
		return this.excelMeta.getMaxAllowedErrors();
	}
	
	/** 获取指定名称的sheet的数据消费者	
	 * @author chaowangbang
	 * @since Mar 26, 2012
	 * @param sheetName
	 * @return
	 */
	public IExcelDataConsumer getExcelDataConsumer(String sheetName) {
		SheetMeta sheetMeta = getSheetMeta(sheetName);
		String consumerBean = sheetMeta.getConsumerBean();
		return Ccay.getContext().getBean(consumerBean, IExcelDataConsumer.class);
	}
	
	/** 设置ResourceBundle	
	 * @author chaowangbang
	 * @since Mar 26, 2012
	 * @param resourceBundle
	 */
	public void setResourceBundle(ResourceBundle resourceBundle) {
		this.resourceBundle = resourceBundle;
	}
	
	/** 获取ResourceBundle	
	 * @author chaowangbang
	 * @since Mar 26, 2012
	 * @return
	 */
	public ResourceBundle getResourceBundle() {
		return this.resourceBundle;
	}
	
	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.IExcelImportContext#getExcelMeta()
	 */
	public ExcelMeta getExcelMeta() {
		return this.excelMeta;
	}
	
	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.IExcelImportContext#setExcelMeta(org.ccay.excel.meta.ExcelMeta excelMeta)
	 */
	public void setExcelMeta(ExcelMeta excelMeta) {
		this.excelMeta = excelMeta;
	}
	/**
	 * @param sheetMeta
	 */
	public void setCurrentSheet(SheetMeta sheetMeta) {
		this.sheetMeta = sheetMeta;
	}
	
	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.IExcelImportContext#getCurrentSheet()
	 */
	public SheetMeta getCurrentSheet() {
		return this.sheetMeta;
	}
	
	/**
	 * @param dataConsumer
	 */
	public void setCurrentDataConsumer(IExcelDataConsumer dataConsumer) {
		this.dataConsumer = dataConsumer;
	}
	
	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.IExcelImportContext#getCurrentDataConsumer()
	 */
	public IExcelDataConsumer getCurrentDataConsumer() {
		return this.dataConsumer;
	}
	
	/**
	 * @param titleMapper
	 */
	public void setTitleMapper(ITitleMapper titleMapper) {
		this.titleMapper = titleMapper;
	}
	
	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.IExcelImportContext#getTitleMapper()
	 */
	public ITitleMapper getTitleMapper() {
		return this.titleMapper;
	}
	
	/**
	 * 重置上下文
	 */
	public void reset() {
		this.sheetMeta = null;
		this.dataConsumer = null;
		this.titleMapper = null;
	}

	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.IExcelImportContext#addImportedCount(int)
	 */
	public void addImportedCount(int count) {
		this.importedCount += count;
	}

	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.IExcelImportContext#getImportedCount()
	 */
	public int getImportedCount() {
		return this.importedCount;
	}

	/**
	 * 设置已经处理的行数 零时表导入使用
	 * @param count
	 * @return
	 * Created：2012-9-12
	 * @author chaowangbang
	 */
	public void setImportedCount(int count){
		//如果设置的导入条数 大于处理的条数 那么使用处理的条数
		if(count>this.processedCount){
			this.importedCount = this.processedCount;
		}else{
			this.importedCount = count;
		}
	}
	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.IExcelImportContext#addProcessedCount(int)
	 */
	public void addProcessedCount(int count) {
		this.processedCount += count;
	}
	/*
	 * (non-Javadoc)
	 * @see org.ccay.excel.importer.IExcelImportContext#addProcessedCount(int)
	 */
	public void setProcessedCount(int count) {
		this.processedCount = count;
	}

	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.IExcelImportContext#getProcessedCount()
	 */
	public int getProcessedCount() {
		return this.processedCount;
	}

	public List<ExcelCellError> getErrors() {
		return this.errors;
	}

	/**
	 * 添加单个错误信息 给错误列表
	 */
	public void addCellError(ExcelCellError error)
			throws ExcelValidationException {
		this.errors.add(error);
		if(this.maxAllowedErrors >0 ) {//需要处理最大错误数
			if(errors.size()>=maxAllowedErrors) {
				throw new ExcelValidationException("ccay.excel.00060017", maxAllowedErrors);
			}
		}
	}
	/**
	 * 添加批量的错误信息给错误列表
	 */
	public void addCellErrors(List<ExcelCellError> errors)
			throws ExcelValidationException {
		this.errors.addAll(errors);
		if(this.maxAllowedErrors >0 ) {//需要处理最大错误数
			if(errors.size()>=maxAllowedErrors) {
				throw new ExcelValidationException("ccay.excel.00060017", maxAllowedErrors);
			}
		}
	}

	public Map<?, ?> getConsumerDataMap() {
		return this.consumerDataMap;
	}

	public void setConsumerDataMap(Map<?, ?> dataMap) {
		this.consumerDataMap = dataMap;
	}

	public Map<String, String> getNameDisplayNameMap() {
		return nameDisplayNameMap;
	}

	public void setNameDisplayNameMap(Map<String, String> nameDisplayNameMap) {
		this.nameDisplayNameMap = nameDisplayNameMap;
	}


	
}
